<?php
namespace Partner\Controller;

use Utuo\BackgroundController;

class ProductPayApiController extends BackgroundController {
	
	public function _initialize() {
		$this->setControllerTitle("支付接口管理");
		$this->setActionTitle(array(
			'index' => "支付方式列表",
			'add' => "支付方式添加",
			'modify' => "修改支付配置",
			'recycle' => "回收站"
		));
		$this->setActionNav("index,add,recycle");
	}
	
	
	/**
	 * 支付方式列表
	 */
	public function indexAction() {
		$this->getProductPayApiList(0);
	}
	
	/**
	 * 添加支付方式配置
	 */
	public function addAction() {
		C("TOKEN_ON", true);
		$this->showLinkNav();
		$tag = I("tag");
		$payMethod = $this->getAddablePayApi();
		if(empty($tag) or !in_array($tag, array_keys($payMethod))) {
			$tag = key($payMethod);
			$config = current($payMethod);
			!empty($payMethod) and $tag;
		}
		if(!$tag) $error = "没有可添加的接口了。";
		else{
			$d = D("ProductPayApi");
			$payConfig = $d->payMethod[$tag];
			$productPayApi['tag'] = $tag;
			$productPayApi["cfg"] = $payConfig;
			$productPayApi["status"] = $this->getRadio("status", $d->status, 1);
			$this->assign("productPayApi", $productPayApi);
		}
		$this->assign("error", $error);
		$this->display("modify");
	}
	
	/**
	 * 修改支付方式配置
	 */
	public function modifyAction() {
		$this->showLinkNav();
		$id = I("id", 0, "intval");
		$d = D("ProductPayApi");
		$id and $productPayApi = $d->find($id) and $cfg = $productPayApi['cfg'];
		$cfg = (array)unserialize($cfg);
		$payConfig = $d->payMethod[$productPayApi["tag"]];
		foreach ($payConfig['fields'] as &$v) {
			$v['value'] = $cfg[$v['name']];
		}
		$productPayApi["cfg"] = $payConfig;
		$productPayApi["status"] = $this->getRadio("status", $d->status, $productPayApi['status']);
		$this->assign("productPayApi", $productPayApi);
		$this->display("modify");
	}
	
	/**
	 * 添加支付方式配置
	 */
	public function savePostAction() {
		$productPayApi = D("ProductPayApi");
		false !== $productPayApi->submit() or $this->ajaxError($productPayApi->getError());
		$this->ajaxSuccess();
	}
	
	/**
	 * 回收站
	 */
	public function recycleAction() {
		$this->getProductPayApiList(1);
	}
	
	/**
	 * 设置状态
	 */
	public function setStatusPostAction() {
		$this->ajaxSetStatus();
	}
	
	/**
	 * 移除配置
	 */
	public function removePostAction() {
		$this->ajaxRemove();
	}
	
	/**
	 * 删除配置
	 */
	public function deletePostAction() {
		$this->ajaxDelete();
	}
	
	/**
	 * 恢复配置
	 */
	public function restorePostAction() {
		$this->ajaxRestore();
	}
	
	private function getProductPayApiList($is_deleted = 0) {
		$productPayApiLists = M("ProductPayApi")->where("pid=%d and is_deleted=%d", $this->aid, $is_deleted)->select();
		$this->assign("productPayApiLists", $productPayApiLists);
		$this->display();
	}
	
	/**
	 * 显示可添加的接口导航
	 */
	private function showLinkNav() {
		$payMethod = $this->getAddablePayApi();
		foreach ($payMethod as $k => $v) {
			$this->setLinkNav($v['name'], U("ProductPayApi/add?tag={$k}"));
		}
	}
	
	
	/**
	 * 获得可添加的api配置
	 * @return array
	 */
	private function getAddablePayApi() {
		$d = D("ProductPayApi");
		$payMethod = $d->payMethod;
		$tags = (array)$d->where("pid=%s", $this->aid)->getField("tag", true);
		foreach ($payMethod as $k => $v) {
			if(in_array($k, $tags)) {
				unset($payMethod[$k]);
			}
		}
		return $payMethod;
	}
	
}